﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using YJKOpeningCommon.OpeningEX;

namespace YJKOpeningCreate.GetOpening
{
	internal class Common
	{
		public static int RelateNameOpening(Autodesk.Revit.DB.Document doc, string strOpeningSpecialty)
		{
			Common.m_dicOpeningNum.Clear();
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_GenericModel).WhereElementIsNotElementType();
			OpeningXData openingXData = new OpeningXData();
			if (openingXData != null)
			{
				foreach (Element ele in filteredElementCollector)
				{
					Common.AddOpening(ele, openingXData);
				}
				foreach (string text in Common.m_dicOpeningNum.Keys)
				{
					if (text == openingXData.m_strOpeningName)
					{
						return Common.m_dicOpeningNum[text];
					}
				}
				return 0;
			}
			return 0;
		}

		private static Dictionary<string, int> AddOpening(Element ele, OpeningXData openingXData)
		{
			if (!openingXData.ReadOpeningXData(ele))
			{
				return Common.m_dicOpeningNum;
			}
			try
			{
				if (Common.m_dicOpeningNum.ContainsKey(openingXData.m_strOpeningName))
				{
					int num = Common.m_dicOpeningNum[openingXData.m_strOpeningName];
					string[] array = openingXData.m_strOpeningName.Split(new string[]
					{
						"_"
					}, StringSplitOptions.RemoveEmptyEntries);
					int num2 = 0;
					if (int.TryParse(array[1], out num2) && num2 > num)
					{
						Common.m_dicOpeningNum[openingXData.m_strOpeningName] = num2;
					}
				}
				else
				{
					Common.m_dicOpeningNum.Add(openingXData.m_strOpeningName, 1);
				}
			}
			catch
			{
				return Common.m_dicOpeningNum;
			}
			if (Common.m_dicExistOpening.ContainsKey(openingXData.m_strOpeningName))
			{
				Common.m_dicExistOpening[openingXData.m_strOpeningName].Add(ele);
			}
			else
			{
				List<Element> list = new List<Element>();
				list.Add(ele);
				Common.m_dicExistOpening.Add(openingXData.m_strOpeningName, list);
			}
			return Common.m_dicOpeningNum;
		}

		private static Dictionary<string, List<Element>> m_dicExistOpening = new Dictionary<string, List<Element>>();

		private static Dictionary<string, int> m_dicOpeningNum = new Dictionary<string, int>();
	}
}
